package com.wentao.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wentao.mall.commons.Constant;
import com.wentao.mall.exception.MallException;
import com.wentao.mall.exception.MallExceptionEnum;
import com.wentao.mall.mapper.CategoryMapper;
import com.wentao.mall.mapper.ProductMapper;
import com.wentao.mall.model.dto.ProductListRequest;
import com.wentao.mall.model.po.Product;
import com.wentao.mall.model.query.ProductQuery;
import com.wentao.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void addProduct(Product product) {
        Product oldProduct = productMapper.selectByName(product.getName());
        if (oldProduct!=null){
            throw new MallException(MallExceptionEnum.NAME_ALREADY_EXISTS);
        }
        int count = productMapper.insertSelective(product);
        if (count==0){
            throw new MallException(MallExceptionEnum.USER_INSERT_FAILED);
        }
    }

    @Override
    public void updateProduct(Product product) {
        if (product.getName()!=null){
            Product selectProduct = productMapper.selectByName(product.getName());
            if(selectProduct!=null&&selectProduct.getId()!=product.getId()){
                throw new MallException(MallExceptionEnum.NAME_ALREADY_EXISTS);
            }
        }
        int count = productMapper.updateByPrimaryKeySelective(product);
        if (count==0) {
            throw new MallException(MallExceptionEnum.UPDATE_FAINED);
        }
    }

    @Override
    public void deleteProduct(Integer id) {
        Product product = productMapper.selectByPrimaryKey(id);
        if (product==null){
            throw new MallException(MallExceptionEnum.DELETE_FAILED);
        }
        int count = productMapper.deleteByPrimaryKey(id);
        if (count==0) {
            throw new MallException(MallExceptionEnum.DELETE_FAILED);
        }
    }

    @Override
    public void batchUpdateSellStatus(Integer[] ids, Integer sellStatus) {
        productMapper.batchUpdateSellStatus(ids,sellStatus);
    }

    @Override
    public Product detail(Integer id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo listForAdmin(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize,"update_time desc");
        List<Product> products = productMapper.selectByExample(null);
        PageInfo pageInfo =new PageInfo(products);
        return pageInfo;
    }

    @Override
    public PageInfo listForCustomer(ProductListRequest productListRequest) {
        ProductQuery productQuery = new ProductQuery();
        //处理keyword
        if (StringUtils.hasText(productListRequest.getKeyword())){
            String keyword=new StringBuffer().append("%")
                    .append(productListRequest.getKeyword())
                    .append("%").toString();
            productQuery.setKeyword(keyword);
        }
        //处理categoryId,用户传入categoryId不仅要查出当前分类下的产品，也要查询出其子分类下的产品,当前categoryId作为二级目录
        //比如查询新鲜水果分类，要找出新鲜水果分类下的苹果，桃子，西瓜等分类的商品
        if (productListRequest.getCategoryId()!=null){
            List<Integer> categoryIds = getCategoryIds(productListRequest.getCategoryId());
            productQuery.setCategoryIds(categoryIds);
        }
        //处理排序
        String orderBy=productListRequest.getOrderBy();
        if (Constant.ProductListOrderBy.PRICE_ASC_DESC.contains(orderBy)){
            PageHelper.startPage(productListRequest.getPageNum(),productListRequest.getPageSize(),orderBy);
        }
        else {
            PageHelper.startPage(productListRequest.getPageNum(),productListRequest.getPageSize());
        }
        List<Product> list = productMapper.selectListByProductQuery(productQuery);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    private List<Integer> getCategoryIds(Integer categoryId) {
        List<Integer> list =categoryMapper.selectCategoryIdsByParentId(categoryId);
        list.add(categoryId);
        return list;
    }
}
